set more 1
set scheme plotplain
graph set window fontface "Times New Roman"
use ..\data\paper_data.dta, clear
drop if treatment_id==3

global style_lines_base "lwidth(1.2) lpattern(solid) msym(Sh) msize(2.5) col(g)"
global style_lines_dec "lwidth(1.2) lpattern(longdash) msym(Th) msize(3.5) col(gs10)"
global style_lines_str "lwidth(1.2) lpattern(dash_dot) msym(Oh) msize(3) col(gs5)"
global color_base " white "
global color_dec " gs11 "
global color_str " gs6 "


*Figure 1: Experimental sessions
*===============================

gen base_x=0.36
gen base_y=0.44
gen base_x2=0.16
gen base_y2=0.5
gen dec_x=0.26
gen dec_y=0.34
gen str_x=0.16
gen str_y=0.24
gen y=0
twoway(line y period, color(none)) ///
(rarea base_x base_y period if period<=11, color(black) fcolor(none)) ///
(rarea base_x2 base_y period if period<=5, color(black) fcolor(none)) ///
(rarea base_x base_y period if period<=11, color(black) fcolor(none)) ///
(rarea base_x base_y period if period>10, color(black) fcolor(none)) ///
(rarea dec_x dec_y period if period>=5 & period<=11, color(gs10) fcolor(none)) ///
(rarea dec_x dec_y period if period>10, color(gs10) fcolor(none)) ///
(rarea str_x str_y period if period>=5 & period<=11, color(gs5) fcolor(none)) ///
(rarea str_x str_y period if period>10, color(gs5) fcolor(none)) ///
(pci 0.36 1.05  0.36 4.95, lcolor(white) lwidth(1)) ///
(pci 0.3616 5  0.438 5, lcolor(white) lwidth(1)) ///
, xlab(1(1)25, nolab nogrid) ylab(0(0)0.3, nogrid notick nolab) yscale(lstyle(none)) ytitle("") xtitle(" ") ///
graphregion(color(white) margin(l+34)) plotr(m(0)) ///
text(0.03 3 "Part 1") text(0.03 8 "Part 2") text(0.03 18 "Part 3") ///
text(-0.05 3 "5 periods") text(-0.05 8 "5 periods") text(-0.05 18 "15 periods") ///
text(0.2 0.2 "{bf:Structured session:}", place(w) color(gs5)) text(0.2 3 " Baseline {it:N =} 2 ", color(gs5)) text(0.2 8 " Structured {it:N =} 2 ", color(gs5)) text(0.2 18 " Structured {it:N =} 6 ", color(gs5)) ///
text(0.3 0.2 "{bf:Decentralized session:}", place(w) color(gs10)) text(0.3 3 " Baseline {it:N =} 2 ", color(gs10)) text(0.3 8 " Decentralized {it:N =} 2 ", color(gs10)) text(0.3 18 " Decentralized {it:N =} 6 ", color(gs10)) ///
text(0.4 0.2 "{bf:Baseline session:}", place(w))  text(0.4 3 " Baseline {it:N =} 2 ") text(0.4 8 " Baseline {it:N =} 2 ") text(0.4 18 " Baseline {it:N =} 6 ")  ///
ysize(3) legend(off)
graph export ..\figs\session.pdf, replace

preserve

*Figure 2: Global geoengeering in economies of six
*=================================================

collapse g if part==3, by(treatment_id economy_type economy_id part period)
bysort treatment_id economy_type: egen g_avg=mean(g)
replace g_avg=round(g_avg,0.1)
replace g=15 if g>15
gen ne=3 if treatment_id==0 & g==10
replace ne=3 if treatment_id==0 & g==6 & economy_type==1
replace ne=3 if treatment_id>0 & (g==6 | g==10) & part==3
replace ne=3 if treatment_id>0 & (g==6 | g==10) & part==3
gen high=38
gen low=0
label define treatment_id /// 
  0 "{bf: (a)} Baseline" ///
  1 "{bf: (b)} Decentralized" ///
  2 "{bf: (c)} Structured", replace 
label value treatment_id treatment_id
twoway(hist g if treatment_id==0 & part==3, percent disc fcolor($color_base)) ///
 (hist g if treatment_id==1 & part==3, percent disc bcolor($color_dec)) ///
 (hist g if treatment_id==2 & part==3, percent disc bcolor($color_str)) ///
 (rcap high low g_avg if treatment_id==0 & part==3, color(gs0) lwidth(0.7) msize(0)) /// 
 (rcap high low g_avg if treatment_id==1 & part==3, color(gs0) lwidth(0.7) msize(0)) /// 
 (rcap high low g_avg if treatment_id==2 & part==3, color(gs0) lwidth(0.7) msize(0)) ///
 (scatter high g_avg if treatment_id==0 & part==3, mlabel(g_avg) mlabsize(3.5) mlabcolor(gs0) msym(none)) /// 
 (scatter high g_avg if treatment_id==1 & part==3, mlabel(g_avg) mlabsize(3.5) mlabcolor(gs0) msym(none)) /// 
 (scatter high g_avg if treatment_id==2 & part==3, mlabel(g_avg) mlabsize(3.5) mlabcolor(gs0) msym(none)) ///
 (scatter ne g if treatment_id==0 & part==3, msym(Sh) mcolor(gs0) msize(2)) /// 
 (scatter ne g if treatment_id==1 & part==3 & g==6, msym(T) mcolor(gs0) msize(2)) /// 
 (scatter ne g if treatment_id==2 & part==3 & g==6, msym(O) mcolor(gs0) msize(2)) ///
 (scatter ne g if treatment_id==1 & part==3 & g>6, msym(Th) mcolor(gs0) msize(2)) /// 
 (scatter ne g if treatment_id==2 & part==3 & g>6, msym(Oh) mcolor(gs0) msize(2)), /// 
 by(treatment_id, r(1) note("") legend(off)) subtitle(, size(vlarge) fcolor(white) nobox) plotr(m(sides)) ///
 xlab(0(1)14 15 "    {&ge}15", labsize(medium) nogrid) ylab(0(5)40, labsize(medium)) ///
xtitle("Global geoengineering", size(large)) ytitle("%", size(large)) xsize(4.5) ysize(2)
graph export ..\figs\g_hist_n6.pdf, replace	

restore
preserve

*Figure 3: Higher total earnings with more side-payments
*=======================================================

collapse (mean)offer_taken earnings_tot transfers_sent amount_team if treatment_id>0 & part>1, by(treatment_id economy_id team_id high_gstar period economy_type part)
gen enacted=transfers_sent>0 & transfers_sent<.
replace enacted=. if high_gstar==1 & treatment_id==2
bysort treatment_id part: egen enacted_tot=sum(enacted)
bysort treatment_id: sum enacted_tot if part==2
bysort treatment_id: sum enacted_tot if part==3
di 36+20*2+5*3
di 8+29*2+6*3

collapse (sum)transfers_sent enacted offer_taken earnings_tot, by(treatment_id economy_id part)
bysort treatment_id part: egen offer_taken_tot=sum(offer_taken)

* (a) N=2 
twoway(lfit earnings_tot offer_taken if part==2 & treatment_id==1, $style_lines_dec) ///
(lfit earnings_tot offer_taken if part==2 & treatment_id==2, $style_lines_str lpattern(solid) lcol(black)) ///
(scatter earnings_tot offer_taken if part==2 & treatment_id==1, msym(Th) col($color_dec) msize(3)) ///
(scatter earnings_tot offer_taken if part==2 & treatment_id==2, msym(Oh) col(black) msize(3)), ///
ytitle("Total earnings", size(vlarge)) xtitle("Total number of side-payments", size(vlarge)) legend(order(3 "Decentralized" 4 "Structured") pos(5) ring(0) row(2) size(medlarge)) xlab(, labsize(medlarge)) ylab(, labsize(medlarge)) 
graph export ..\figs\earnings_offers_n2.pdf, replace
bysort treatment_id: reg earnings_tot offer_taken if part==2
bysort treatment_id: sum offer_taken_tot if part==2

* (b) N=6
twoway(lfit earnings_tot offer_taken if part==3 & treatment_id==1, $style_lines_dec) ///
(lfit earnings_tot offer_taken if part==3 & treatment_id==2, $style_lines_str lpattern(solid) lcol(black)) ///
(scatter earnings_tot offer_taken if part==3 & treatment_id==1, msym(Th) col($color_dec) msize(3)) ///
(scatter earnings_tot offer_taken if part==3 & treatment_id==2, msym(Oh) col(black) msize(3)), ///
ytitle("Total earnings", size(vlarge)) xtitle("Total number of side-payments", size(vlarge)) legend(off) ///
xlabel(0(5)25, labsize(medlarge)) ylab(, labsize(medlarge)) 
graph export ..\figs\earnings_offers_n6.pdf, replace

bysort treatment_id: sum earnings_tot if part==3
bysort treatment_id: reg earnings_tot offer_taken if part==3
bysort treatment_id: sum offer_taken if part==3
bysort treatment_id: sum offer_taken_tot if part==3
di 0.64*90 // Decentralized in Tab 3
di 0.28*150 // Structured in Tab 3

restore
preserve

*Figure 4: Offers and side-payments over time
*============================================

collapse high_gstar gstar target_team offer_made offer_taken if part>1 & treatment_id>0, by(treatment_id economy_id team_id part period)
gen offer_made_highip=offer_made if treatment_id==2
replace offer_made_highip=offer_made if treatment_id==1 & part==2 & high_gstar==0
replace offer_made_highip=offer_made if treatment_id==1 & part==3 & high_gstar==0 & target_team>=5 & target_team<.
bysort economy_id period: egen n_offers_made_highip=sum(offer_made_highip)
bysort economy_id period: egen n_offers_made_all=sum(offer_made)
bysort economy_id period: egen n_offers_fulfilled_all=sum(offer_taken)
collapse (max)offer_made offer_taken n_*, by(treatment_id economy_id part period)
replace offer_taken=0 if offer_taken==.
replace n_offers_fulfilled_all=0 if n_offers_fulfilled_all==.
collapse offer_made offer_taken n_offers_*, by(treatment_id part period)

label define treatment_id /// 
  1 "{bf: (a)} Decentralized" ///
  2 "{bf: (b)} Structured", replace 
label value treatment_id treatment_id
twoway (line n_offers_made_all period if period<=10, lpattern(longdash) lcolor(gs9) lwidth(0.7)) ///
(line n_offers_made_all period if period> 10, lpattern(longdash) lcolor(gs9) lwidth(0.7)) ///
(line n_offers_fulfilled_all period if period<=10, lcolor(black) lpattern(solid) lwidth(0.7)) ///
(line n_offers_fulfilled_all period if period> 10, lcolor(black) lpattern(solid) lwidth(0.7)) ///
(line n_offers_made_highip period if period<=10 & treatment_id==1, lcolor(gs5) lpattern(shortdash) lwidth(0.7)) ///
(line n_offers_made_highip period if period> 10 & treatment_id==1, lcolor(gs5) lpattern(shortdash) lwidth(0.7)), ///
by(treatment_id, note("")) xtitle("Period", size(large)) subtitle(, size(vlarge) fcolor(white) nobox) ytitle("Average number", size(large)) xlab(6 10(5)25, labsize(medlarge) nogrid) ylab(, labsize(medlarge)) xsize(4) ysize(2) ///
legend(order(1 "Offer" 5 "Offer to highest-ideal-point teams" 3 "Side-payment") r(1) size(large)) 
graph export ..\figs\amount_round.pdf, replace 

restore
preserve

*Figure 5: Were offers profitable for the receiver?
*==================================================

keep if part==2 & (treatment_id==1 | treatment_id==2)
collapse (mean)gstar amount_team request_team sign_team gstar_opponent if high_gstar==0 & offer_made==1, by(treatment_id economy_id team_id period economy_type)
append using ..\data\predictions.dta

replace request_team=request_team-1 if sign_team==-1
replace request_team=gstar_opponent if sign_team==1 & request_team<gstar_opponent
gen pred_dec=34 if request_team==2 & economy_type==1
replace pred_dec=70 if request_team==2 & economy_type==2
replace pred_dec=34 if request_team==6 & economy_type==3

label define economy_type /// 
  1 "{bf: (a)} Economy ({it:L}, {it:M})" /// 
  2 "{bf: (b)} Economy ({it:L}, {it:H})" /// 
  3 "{bf: (c)} Economy ({it:M}, {it:H})", replace 
label value economy_type economy_type
sort amount_team
twoway(area amount_team request_team if first_profitable==1, sort col(gs12)) ///
(scatter pred_dec request_team, msym(O) col(black) msize(4.5)) ///
(scatter amount_team request_team if treatment_id==1, jitter(2) msym(Th) col(gs4) msize(2)) ///
(scatter amount_team request_team if treatment_id==2, jitter(2) msym(Oh) col(gs8) msize(2)), ///
by(economy_type, r(1) note(""))  subtitle(, size(vlarge) fcolor(white) nobox) xtitle("Request", size(large)) ytitle("Amount", size(large)) ///
legend(col(2) order(4 "Empirical observation in Structured" 2 "Theoretical prediction in Structured" 3 "Empirical observation in Decentralized" 1 "Unprofitable offer") size(medlarge)) xlab(, labsize(medium)) ylab(, labsize(medium)) xsize(4.5) ysize(2)
graph export ..\figs\offers_type.pdf, replace

restore

*Figure 6: Chat activity by treatment and experience
*===================================================

use ..\data\chat.dta, clear

bysort subject_id (period): gen economy_id_n6=economy_id[_N]
drop if time==.
gsort treatment_id subject_id period effort_stage -time

gen time2=120-time if part==1
replace time2=60-time if part==2
replace time2=120-time if part==2 & treatment_id> 0 & effort_stage==0
replace time2=120-time if part==3 & treatment_id==0
replace time2=60-time  if part==3 & treatment_id==0 & period>18
replace time2=120-time if part==3 & treatment_id> 0 & effort_stage==0
replace time2=60-time  if part==3 & treatment_id> 0 & effort_stage==1
replace time2=60-time  if part==3 & treatment_id> 0 & effort_stage==0 & period>18
replace time2=30-time  if part==3 & treatment_id> 0 & effort_stage==1 & period>18

gen words=wordcount(text)
gen words_1=wordcount(text) if effort_stage==1 & time2<=60 & (period==5 | (period==18 & treatment_id==0))
replace words_1=wordcount(text) if effort_stage==1 & time2<=30 & (period==18 & treatment_id>0)
gen words_2=wordcount(text) if effort_stage==1 & time2 >60 & (period==5 | (period==18 & treatment_id==0))
replace words_2=wordcount(text) if effort_stage==1 & time2>30 & (period==18 & treatment_id>0)
gen words_1_offer=wordcount(text) if effort_stage==0 & time2<=60 & period==18 & treatment_id>0
gen words_2_offer=wordcount(text) if effort_stage==0 & time2 >60 & period==18 & treatment_id>0
/*
preserve

gen moment=0 & part==1
replace moment=1 if time2>60 & part==1
collapse (sum)words, by(treatment_id economy_id part period moment)
graph bar words if part==1, over(moment) over(period) by(treatment_id, r(3)) 
bysort treatment_id moment: sum words

restore
*/
preserve

collapse (sum)words*, by(treatment_id economy_id_n6 part period effort_stage) // economy_n6-stage level dataset
sort treatment_id economy_id_n6 period effort_stage

gen share_1=words_1/words
gen share_1_offer=words_1_offer/words
sum share_1 if period==5
gen share_1_p5=r(mean)
sum share_1 if period==18 & treatment_id==0
gen share_1_p18_base=r(mean)
sum share_1 if period==18 & treatment_id==1 & effort_stage==1
gen share_1_p18_dec=r(mean)
sum share_1 if period==18 & treatment_id==2 & effort_stage==1
gen share_1_p18_str=r(mean)
sum share_1_offer if period==18 & treatment_id==1 & effort_stage==0
gen share_1_offer_p18_dec=r(mean)
sum share_1_offer if period==18 & treatment_id==2 & effort_stage==0
gen share_1_offer_p18_str=r(mean)

gen words_share=words
replace words_share=words/share_1_p5 if part==2
replace words_share=words/share_1_p18_base if period>18 & effort_stage==1 & treatment_id==0
replace words_share=words/share_1_p18_dec  if period>18 & effort_stage==1 & treatment_id==1
replace words_share=words/share_1_p18_str  if period>18 & effort_stage==1 & treatment_id==2
replace words_share=words/share_1_offer_p18_dec  if period>18 & effort_stage==0 & treatment_id==1
replace words_share=words/share_1_offer_p18_str  if period>18 & effort_stage==0 & treatment_id==2

replace part=4 if period>15
collapse (mean)words_share, by(treatment_id part effort_stage)

gen xaxis=1 if treatment_id==0 & part==1
replace xaxis=2 if treatment_id==1 & part==1
replace xaxis=3 if treatment_id==2 & part==1
replace xaxis=5 if treatment_id==0 & part==2
replace xaxis=6 if treatment_id==1 & part==2
replace xaxis=7 if treatment_id==2 & part==2
replace xaxis=9 if treatment_id==0 & part==3
replace xaxis=10 if treatment_id==1 & part==3
replace xaxis=11 if treatment_id==2 & part==3
replace xaxis=13 if treatment_id==0 & part==4
replace xaxis=14 if treatment_id==1 & part==4
replace xaxis=15 if treatment_id==2 & part==4
gen offer_stage=(effort_stage==0)
bysort treatment_id part (offer_stage): gen words2_share=sum(words_share)
gen word_label_pos_share=words2_share+10
twoway ///
(bar words2_share xaxis if treatment_id==0, fcolor($color_base) lcolor(black)) ///
(bar words2_share xaxis if treatment_id==1, fcolor(gs12) lcolor(black)) ///
(bar words2_share xaxis if treatment_id==1 & offer_stage==0, fcolor($color_transfer) lcolor(black)) ///
(bar words2_share xaxis if treatment_id==2, fcolor(gs4) lcolor(black)) ///
(bar words2_share xaxis if treatment_id==2 & offer_stage==0, fcolor($color_structured) lcolor(black)) ///
(scatter word_label_pos_share xaxis, msym(none) mlabel(words2_share) mlabcol(black) mlabp(c) mlabf(%9.0f)) ///
(scatter word_label_pos_share xaxis if treatment_id==2 & effort_stage==0, msym(none) mlabel(words2_share) mlabcol(black) mlabp(c) mlabf(%9.0f)), ///
ylabel(0(50)500) xlabel(0 " " 2 `""Part 1" "(periods 1-5)""' 6 `""Part 2" "(periods 6-10)""' 10 `""Part 3" "(periods 11-15)""' 14 `""Part 3" "(periods 16-25)""' 16 " ", notick nogrid labsize(medsmall)) xtitle("") ytitle("Number of words in an economy per period") legend(off) text(275 1 "Baseline", orient(vertical))  text(300 2 "Decentralized", orient(vertical)) text(285 3 "Structured", orient(vertical)) text(300 6 "Offer stage", orient(vertical)) text(185 7 "Offer stage", orient(vertical))
graph export ..\figs\words_volume.pdf, replace